package com.lark.utils;

import atlantafx.base.theme.*;
import com.alibaba.fastjson2.JSONObject;
import com.lark.constant.FileConstant;
import com.lark.context.ApplicationContext;
import com.lark.model.ClientModel;
import com.lark.model.SettingModel;
import com.lark.view.SplashScreen;
import javafx.geometry.Rectangle2D;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Objects;
import java.util.Properties;

import static com.lark.context.ApplicationContext.*;

@Slf4j
public final class InitUtil {

    private InitUtil() {
    }

    /**
     * 初始化配置
     */
    public static void initConfigure() throws IOException {
        // 加载配置文件
        Properties properties = PropertiesUtil.parseResourceFile("application.properties");
        clientModel = new ClientModel(properties.getProperty("client"), properties.getProperty("client.version"),
                properties.getProperty("client.description"), properties.getProperty("client.url"), properties.getProperty("default.config.path"),
                properties.getProperty("default.config.file"), properties.getProperty("author"), properties.getProperty("author.email"),
                properties.getProperty("author.url"), properties.getProperty("organization"),
                properties.getProperty("organization.url"));
        // 默认配置文件路径
        Path folderPath = Paths.get(FileConstant.DEFAULT_CONF_PATH, clientModel.defaultConfPath());
        if (!Files.exists(folderPath)) {
            // 创建文件夹
            Files.createDirectories(folderPath);
            log.info("Initialize folder [{}] success...", folderPath);
        }
        Path filePath = Paths.get(FileConstant.DEFAULT_CONF_PATH, clientModel.defaultConfPath(), clientModel.defaultConfFile());
        if (!Files.exists(filePath)) {
            // 文件不存在，创建文件，并初始化数据
            Files.createFile(filePath);
            log.info("Initialize file [{}] success...", filePath);
            settingModel = new SettingModel();
            settingModel.setTheme(new PrimerDark().getUserAgentStylesheet());
            settingModel.setWidth(550d);
            settingModel.setHeight(260d);
            settingModel.setTrayEnabled(true);
            Files.writeString(filePath, JSONObject.toJSONString(settingModel));
        } else {
            // 加载setting目录下的配置文件
            settingModel = JsonUtil.parseJsonToClass(FileUtil.parse(filePath.toString()), SettingModel.class);
            log.info("Load configuration file [{}] success...", filePath);
        }
        // 设置项目配置信息
        ApplicationContext.theme = Objects.requireNonNullElse(settingModel.getTheme(), new PrimerLight().getUserAgentStylesheet());
    }

    /**
     * 启动动画
     *
     * @return
     */
    public static Stage initSplashScreenAnimation(SplashScreen splashScreen) {
        final Stage splashStage = new Stage(StageStyle.TRANSPARENT);
        if (splashScreen.visible()) {
            // 创建一个无边框的Stage（不会在任务栏展示），作为splashStage的拥有者
            final Stage noBarStage = new Stage(StageStyle.UTILITY);
            noBarStage.setX(Double.MAX_VALUE);
            noBarStage.show();
            // 设置splashStage的拥有者
            splashStage.initOwner(noBarStage);

            final Scene splashScene = new Scene(splashScreen.getParent(), Color.TRANSPARENT);
            splashScene.setCamera(new PerspectiveCamera());
            splashStage.setScene(splashScene);
            splashStage.show();
        }
        return splashStage;
    }
}
